<?php
namespace yphp;
/**
 * 会话SESSION
 */
class Session extends Instance
{
    // session状态
    static $init;

    // 初始化配置
    static function init()
    {
        if (is_null(self::$init)){
            $config = Config::session();
            foreach ($config['ini_set'] as $k => $v){
                ini_set('session'.$k,$v);
            }
            switch ($config['ini_set']['save_handler']){
                case 'files':
                    ini_set('session.save_path',APP_SESSION);
                    break;
                case 'memcache':
                    self::SaveMemcache(Memcache::node());
                    break;
                case 'memcached':
                    self::SaveMemcache(Memcached::node());
                    break;
                case 'redis':
                    self::SaveRedis(Config::nosql('redis.master'));
                    break;
                default:
                    throw new \Exception('没有这个存储选项');
            }

            session_name(Safe::en($config['session_name']));
            ini_set('session.gc_maxlifetime',$config['expire']);
            ini_set('session.cookie_lifetime',$config['expire']);
            self::$init = true;
        }
    }

    // 开启session
    static function start()
    {
        self::init();
        isset($_SESSION) || session_start();
    }

    // 获取session
    static function get($key = false)
    {
        self::start();
        return $key ? key_exists($key,$_SESSION) ? $_SESSION[$key] : null : $_SESSION;
    }

    // 设置session  Session::set('user.id',1)
    static function set($key,$val)
    {
        self::start();
        $_SESSION[$key] = $val;
    }

    // 删除session
    static function del($key = false)
    {
        self::start();
        if ($key){
            if (key_exists($key,$_SESSION)){
                unset($_SESSION[$key]);
            }
        } else {
            session_destroy();
        }
    }

    // session_id
    static function id()
    {
        self::start();
        return session_id();
    }

    // 更新session_id
    static function renew($force = false)
    {
        self::init();
        session_regenerate_id($force);
    }

    // 获取/设置session名称
    static function name()
    {
        self::init();
        return call_user_func_array('session_name', func_get_args());
    }

    //+---------------------------------------------------------
    // 会话存储方式
    //+---------------------------------------------------------

    // memcache
    static function SaveMemcache($nodes)
    {
        $save_path = '';
        foreach ($nodes as $k => $v){
            $save_path = 'tcp://'.$k.'?weight='.$v.';';
        }
        if($save_path === '') throw new \Exception('memcache配置异常');
        ini_set('session.save_path',rtrim($save_path,';'));
    }

    // memcached
    static function SaveMemcached($nodes)
    {
        $save_path = '';
        foreach ($nodes as $k => $v){
            $save_path = 'tcp://'.$k.'?weight='.$v.';';
        }
        if($save_path === '') throw new \Exception('memcached配置异常');
        ini_set('session.save_path',rtrim($save_path,';'));
    }

    // redis
    static function SaveRedis($nodes)
    {
        ini_set('session.save_path','tcp://'.$nodes);
    }

}